﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReactReduxTool.Utils {

    public class CmdUtil {

        private static CmdUtil cmdUtil;

        public static CmdUtil GetInstance() {
            return new CmdUtil();
        }

        public delegate void OnPublishCmdResultListener(string result);

        private Process mProcess;

        private CmdUtil() {
            mProcess = new Process();
        }

        /// <summary>
        /// 执行CMD命令
        /// </summary>
        /// <param name="command"></param>
        /// <param name="listener"></param>
        public void RunCmd(string command, OnPublishCmdResultListener listener)
        {
            mProcess.StartInfo.CreateNoWindow = true;
            mProcess.StartInfo.FileName = "cmd.exe";
            mProcess.StartInfo.UseShellExecute = false;
            mProcess.StartInfo.RedirectStandardError = true;
            mProcess.StartInfo.RedirectStandardInput = true;
            mProcess.StartInfo.RedirectStandardOutput = true;
            mProcess.Start();
            //mProcess.StandardInput.WriteLine("@echo off");
            //mProcess.StandardInput.WriteLine("cls");
            mProcess.StandardInput.WriteLine(command);
            mProcess.StandardInput.WriteLine("exit");
            string outStr = mProcess.StandardOutput.ReadToEnd();
            mProcess.Close();
            listener(Encoding.UTF8.GetString(Encoding.GetEncoding("GBK").GetBytes(outStr)));
        }

    }
}
